package com.baby.movie.service;


import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.ResponseHandlerInterface;

public final class MTService {
	
	private static final String BASE_URL = "http://api.baobeiya.cn/api/Mt";
	
	private static final AsyncHttpClient CLIENT = new AsyncHttpClient();

	/**
	 * 推荐视频
	 * @param channelId 视频类型id
	 * @param responseHandler
	 * @return
	 */
	public static RequestHandle getRecommend(int channelId, ResponseHandlerInterface responseHandler) {
		String url = BASE_URL + "/GetRecommend?channelid=" + channelId;
		return CLIENT.get(url, responseHandler);
	}
	
	/**
	 * 分类视频
	 * @param page
	 * @param responseHandler
	 * @return
	 */
	public static RequestHandle getChannelVideo(int page, ResponseHandlerInterface responseHandler) {
		String url = BASE_URL + "/GetVideoByChannel";
		MTRequestParams params = new MTRequestParams();
		params.setPage(page);
		return CLIENT.get(url, params, responseHandler);
	}
	
	/**
	 * 获取相关视频，幻灯片视频（用同一个）
	 * @param channelId
	 * @param responseHandler
	 * @return
	 */
	public static RequestHandle getRelativeVideos(int channelId, ResponseHandlerInterface responseHandler) {
		String url = BASE_URL + "/GetCorrelationVideo?channelId=" + channelId;
		MTRequestParams params = new MTRequestParams();
		params.setPage(1);
		params.setCount(5);
		return CLIENT.get(url, params, responseHandler);
	}
	
	/**
	 * 根据channel和类型获取视频，
	 * @param channelId 频道ID
	 * @param type 分类：1：推荐 2：热门 3：最新
	 * @param page
	 * @param responseHandler
	 * @return
	 */
	public static RequestHandle getChannelVideoByType(int channelId, int type, int page, ResponseHandlerInterface responseHandler) {
		String url = BASE_URL + "/GetchannelVideoByType?channelId=" + channelId;
		MTRequestParams params = new MTRequestParams();
		params.put("channelId", channelId);
		params.setPage(page);
		return CLIENT.get(url, params, responseHandler);
	}
	
	/**
	 * 全部视频
	 * @param page
	 * @param responseHandler
	 * @return
	 */
	public static RequestHandle getVideoAll(int page, ResponseHandlerInterface responseHandler) {
		String url = BASE_URL + "/GetVideoAll";
		MTRequestParams params = new MTRequestParams();
		params.setPage(page);
		return CLIENT.get(url, params, responseHandler);
	}
	
	public static RequestHandle hotSearch(ResponseHandlerInterface responseHandler) {
		String url = BASE_URL + "/HotSearchVideos";
		return CLIENT.get(url, responseHandler);
	}
	
	/**
	 * 视频搜索
	 * @param page
	 * @param categoryId
	 * @param q
	 * @param responseHandler
	 * @return
	 */
	public static RequestHandle search(int page, int categoryId, String q, ResponseHandlerInterface responseHandler) {
		String url = BASE_URL + "/GetSearch";
		MTRequestParams params = new MTRequestParams();
		params.setPage(page);
		params.put("channelid", categoryId);
		params.put("q", q);
		return CLIENT.get(url, params, responseHandler);
	}
	
	
	
	public static void cancel(RequestHandle...handles) {
		for (RequestHandle requestHandle : handles) {
			if (null != requestHandle && !requestHandle.isFinished()) {
				requestHandle.cancel(true);
			}
		}
	}
}
